package com.wwj.admin.wawa.controller;

import com.wwj.admin.BaseCommon;
import com.wwj.core.wawa.entity.LogWaWaAdjust;
import com.wwj.core.wawa.entity.WaWa;
import com.wwj.core.wawa.service.IInventoryService;
import com.wwj.core.wawa.service.ILogWaWaAdjustService;
import com.wwj.framework.utils.Page2EasyUiData;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Auser on 2017/11/16.
 */

@Controller
@RequestMapping("/wawa/inventory")
public class InventoryController extends BaseCommon {

    private final String INDEX = "/admin/wawa/inventory";
    @Resource
    private IInventoryService inventoryService;

    @Resource
    private ILogWaWaAdjustService logWaWaAdjustService;

    @RequestMapping(value = {"","/","/index"})
    public String index()throws Exception{
        return INDEX;
    }

    @RequestMapping("/listData.json")
    @ResponseBody
    public ReturnData list(String search,int offset,int limit)throws Exception{
        Map<String,Object> params = new HashMap<String,Object>();
        if(search != null && !"".equals(search)) params.put("search",search);
        return new ReturnData().success(Page2EasyUiData.format(inventoryService.findByPage(params,offset,limit)));
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ReturnData add(WaWa wawa)throws Exception{
        return inventoryService.add(wawa);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ResponseBody
    public ReturnData add(Integer id)throws Exception{
        if(id == null) return new ReturnData().failure("id不能为空");
        WaWa waWa = inventoryService.findById(id);
        Integer inventory = waWa.getInventory();
        if(inventory > 0) return new ReturnData().failure("库存不为零，请先调整库存为零以后再删除。");
        inventoryService.deleteById(id);
        return new ReturnData().success("删除成功！");
    }

    @RequestMapping("/incoming")
    @ResponseBody
    public ReturnData incoming(WaWa waWa)throws Exception{
        if(waWa.getId() == null) return new ReturnData().failure("id不能空");
        if(waWa.getInventory() == 0) return new ReturnData().failure("入库数不能为空或者0");
        inventoryService.updateIncoming(waWa);
        return new ReturnData().success("入库成功");
    }


    @RequestMapping(value = "/findById/{id}")
    @ResponseBody
    public ReturnData findById(@PathVariable("id") Integer id)throws Exception{
        if(id == null) return new ReturnData().failure("id不能为空");
        return new ReturnData().success(inventoryService.findById(id));
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public ReturnData updateWaWa(WaWa waWa)throws Exception{
        inventoryService.updateWaWa(waWa);
        return new ReturnData().success("更新成功");
    }

    @RequestMapping("/adjust")
    @ResponseBody
    public ReturnData adjust(LogWaWaAdjust logAdjust)throws Exception{
        if(null == logAdjust) return new ReturnData().failure("调整参数不能为空");
        if(null == logAdjust.getWaWaId()) return new ReturnData().failure("娃娃ID不能为空");
        if(null == logAdjust.getAdjustAfter()) return new ReturnData().failure("娃娃库存必须传参数");
        String userId = getShiroUser().getId();
        logAdjust.setAdjustUserId(userId);
        logAdjust.setCreateTime(new Date());
        inventoryService.addAdjustWaWa(logAdjust);
        return new ReturnData().success("库存调整成功");
    }


    @RequestMapping("/adjustListData.json")
    @ResponseBody
    public ReturnData adjustList(String startDate,String stopDate,String search,int offset,int limit)throws Exception{
        Map<String, Object> params = new HashMap<String, Object>();
        if(null != search && !"".equals(search))params.put("search",search);
        if(null != startDate && !"".equals(startDate))params.put("startDate",startDate);
        if(null != stopDate && !"".equals(stopDate))params.put("stopDate",stopDate);
        return new ReturnData().success(Page2EasyUiData.format(logWaWaAdjustService.findByPage(params, offset, limit)));
    }

}
